﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Collections;

namespace SalaryChannel
{
    public partial class PhotoUpdateDialog : UserControl
    {

        public event EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CompletedEvent;
        /// <summary>
        /// Constructer of class Upload
        /// </summary>
        public PhotoUpdateDialog()
        {
            
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceInstance.ViewService.UpdatePhotoCompleted += new EventHandler<SalaryChannel.ViewService.UpdatePhotoCompletedEventArgs>(ViewService_UpdatePhotoCompleted);
        }

        bool _isReady = true;

        private bool IsReady
        {
            get { return _isReady; }
            set { _isReady = value;
                this.Cursor = value? Cursors.Arrow: Cursors.Wait;
            }
        }


        void ViewService_UpdatePhotoCompleted(object sender, SalaryChannel.ViewService.UpdatePhotoCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    App.MessageBox.ShowDialog(Properties.Resource.Succeeded);
                    if (CompletedEvent != null)
                    {
                        CompletedEvent(null, new System.ComponentModel.AsyncCompletedEventArgs(null,false,IsAlbumChanged));
                    }
                }
                else
                {
                    App.MessageBox.ShowDialog(Properties.Resource.TryAgain);
                }
            }
            catch
            {
                App.MessageBox.ShowDialog(Properties.Resource.TryAgain);
            }
            finally
            {
                IsReady = true;
            }
        }

        private string PhotoID = string.Empty;
        private string oldName = string.Empty;
        private string oldDes = string.Empty;
        private int oldAlbumIndex = -1;

        public void BindData(string PhotoID, string PhotoName,
            string Description,int AlbumIndex, List<ViewService.VW_AlbumContains> Albums)
        {
            oldAlbumIndex = AlbumIndex;
            oldDes = Description;
            oldName = PhotoName;
            this.PhotoID = PhotoID;
            this.title.Text = PhotoName;
            description.Text = Description;
            this.albumList.ItemsSource = Albums;
            this.albumList.SelectedIndex = AlbumIndex;
            this.Visibility = Visibility.Visible;

        }

        private bool IsAlbumChanged = false;

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(title.Text))
            {
                App.MessageBox.ShowDialog("名称不能为空");
                return;
            }
            if (albumList.SelectedItem == null)
            {
                App.MessageBox.ShowDialog("请选择一个相册");
                return;
            }
            if (title.Text == oldName
                && albumList.SelectedIndex == oldAlbumIndex
                && description.Text == oldDes)
            {
                App.MessageBox.ShowDialog("您没有做任何修改，无法提交", Title.Warning);
                return;
            }

            IsAlbumChanged = !(albumList.SelectedIndex == oldAlbumIndex);

            try
            {
                ServiceInstance.ViewService.UpdatePhotoAsync(PhotoID,
                       title.Text,  (albumList.SelectedItem as ViewService.VW_AlbumContains).AlbumID,
                       description.Text);
                IsReady = false;
            }
            catch 
            {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.PhotoID = string.Empty;
            this.title.Text = string.Empty;
            description.Text = string.Empty;
            this.albumList.ItemsSource = null;
            IsAlbumChanged = false;
            this.Visibility = Visibility.Collapsed;
        }

     
    }
}
